﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using l1cs.server.model;
using l1cs.server.model.Instance;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class DoorSpawnTable
    {
        private static Logger _log = Logger.getLogger( typeof( DoorSpawnTable ).FullName );

        private static DoorSpawnTable _instance;

        private List<L1DoorInstance> _doorList = new List<L1DoorInstance>();

        public static DoorSpawnTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new DoorSpawnTable();
            }
            return _instance;
        }

        private DoorSpawnTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM spawnlist_door";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1Npc l1npc = NpcTable.getInstance().getTemplate( 81158 );
                    if ( l1npc == null )
                    {
                        continue;
                    }
                    String s = l1npc.getImpl();
                    Type t = Type.GetType( "l1cs.server.model.Instance." + s + "Instance" );
                    L1DoorInstance door = (L1DoorInstance)t.InvokeMember(
                        null, BindingFlags.CreateInstance, null, null, new object[] { l1npc } );
                    door.setId( IdFactory.getInstance().nextId() );
                    door.setDoorId( SqlUtils.toInt( item["id"] ) );
                    door.setGfxId( SqlUtils.toInt( item["gfxid"] ) );
                    door.setX( SqlUtils.toInt( item["locx"] ) );
                    door.setY( SqlUtils.toInt( item["locy"] ) );
                    door.setMap( SqlUtils.toShort( item["mapid"] ) );
                    door.setHomeX( SqlUtils.toInt( item["locx"] ) );
                    door.setHomeY( SqlUtils.toInt( item["locy"] ) );
                    door.setDirection( SqlUtils.toInt( item["direction"] ) );
                    door.setLeftEdgeLocation( SqlUtils.toInt( item["left_edge_location"] ) );
                    door.setRightEdgeLocation( SqlUtils.toInt( item["right_edge_location"] ) );
                    door.setMaxHp( SqlUtils.toInt( item["hp"] ) );
                    door.setCurrentHp( SqlUtils.toInt( item["hp"] ) );
                    door.setKeeperId( SqlUtils.toInt( item["keeper"] ) );

                    L1World.getInstance().storeObject( door );
                    L1World.getInstance().addVisibleObject( door );

                    _doorList.Add( door );
                }
            }
        }

        /// <summary>
        /// ドアリストを返す。
        /// </summary>
        /// <returns></returns>
        public L1DoorInstance[] getDoorList()
        {
            return _doorList.ToArray();
        }
    }
}
